package cn.iocoder.yudao.module.handguard.controller.admin.producttypeinfo;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.handguard.controller.admin.producttypeinfo.vo.*;
import cn.iocoder.yudao.module.handguard.dal.dataobject.producttypeinfo.ProductTypeInfoDO;
import cn.iocoder.yudao.module.handguard.convert.producttypeinfo.ProductTypeInfoConvert;
import cn.iocoder.yudao.module.handguard.service.producttypeinfo.ProductTypeInfoService;

@Tag(name = "管理后台 - 产品类型-贷款基本信息")
@RestController
@RequestMapping("/hutai/product-type-info")
@Validated
public class ProductTypeInfoController {

    @Resource
    private ProductTypeInfoService productTypeInfoService;

    @PostMapping("/create")
    @Operation(summary = "创建产品类型-贷款基本信息")
    @PreAuthorize("@ss.hasPermission('hutai:product-type-info:create')")
    public CommonResult<Long> createProductTypeInfo(@Valid @RequestBody ProductTypeInfoCreateReqVO createReqVO) {
        return success(productTypeInfoService.createProductTypeInfo(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新产品类型-贷款基本信息")
    @PreAuthorize("@ss.hasPermission('hutai:product-type-info:update')")
    public CommonResult<Boolean> updateProductTypeInfo(@Valid @RequestBody ProductTypeInfoUpdateReqVO updateReqVO) {
        productTypeInfoService.updateProductTypeInfo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除产品类型-贷款基本信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('hutai:product-type-info:delete')")
    public CommonResult<Boolean> deleteProductTypeInfo(@RequestParam("id") Long id) {
        productTypeInfoService.deleteProductTypeInfo(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得产品类型-贷款基本信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('hutai:product-type-info:query')")
    public CommonResult<ProductTypeInfoRespVO> getProductTypeInfo(@RequestParam("id") Long id) {
        ProductTypeInfoDO productTypeInfo = productTypeInfoService.getProductTypeInfo(id);
        return success(ProductTypeInfoConvert.INSTANCE.convert(productTypeInfo));
    }

    @GetMapping("/list")
    @Operation(summary = "获得产品类型-贷款基本信息列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('hutai:product-type-info:query')")
    public CommonResult<List<ProductTypeInfoRespVO>> getProductTypeInfoList(@RequestParam("ids") Collection<Long> ids) {
        List<ProductTypeInfoDO> list = productTypeInfoService.getProductTypeInfoList(ids);
        return success(ProductTypeInfoConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得产品类型-贷款基本信息分页")
    @PreAuthorize("@ss.hasPermission('hutai:product-type-info:query')")
    public CommonResult<PageResult<ProductTypeInfoRespVO>> getProductTypeInfoPage(@Valid ProductTypeInfoPageReqVO pageVO) {
        PageResult<ProductTypeInfoDO> pageResult = productTypeInfoService.getProductTypeInfoPage(pageVO);
        return success(ProductTypeInfoConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出产品类型-贷款基本信息 Excel")
    @PreAuthorize("@ss.hasPermission('hutai:product-type-info:export')")
    @OperateLog(type = EXPORT)
    public void exportProductTypeInfoExcel(@Valid ProductTypeInfoExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<ProductTypeInfoDO> list = productTypeInfoService.getProductTypeInfoList(exportReqVO);
        // 导出 Excel
        List<ProductTypeInfoExcelVO> datas = ProductTypeInfoConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "产品类型-贷款基本信息.xls", "数据", ProductTypeInfoExcelVO.class, datas);
    }

}
